package com.uzai.console.service.sop.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.sop.addfanchannel.AddFanChannelDto;
import com.uzai.console.dto.sop.addfanchannel.AddFanChannelIdDto;
import com.uzai.console.dto.sop.addfanchannel.AddFanChannelQuery;
import com.uzai.console.entity.AddFanChannelDetail;
import com.uzai.console.entity.AddFanChannel;
import com.uzai.console.enums.FriendRequestScene;
import com.uzai.console.enums.WxworkFriendRequestScene;
import com.uzai.console.mapper.AddFanChannelDetailMapper;
import com.uzai.console.mapper.AddFanChannelMapper;
import com.uzai.console.service.sop.AddFanChannelService;
import com.uzai.console.vo.sop.addfanchannel.AddFanChannelVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * SOP任务
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class AddFanChannelServiceImpl implements AddFanChannelService {

    @Autowired
    private AddFanChannelMapper addFanChannelMapper;

    @Autowired
    private AddFanChannelDetailMapper addFanChannelDetailMapper;

    /**
     * 查询进粉渠道信息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return AddFanChannelVo
     */
    public Page<AddFanChannelVo> findByPage(AddFanChannelQuery addFanChannelQuery){

        //新建返回到页面的LIST对象
        List<AddFanChannelVo> addFanChannelVoList = new ArrayList<>();

        //执行分页查询对象
        Page<AddFanChannel> page = new Page<>();
        addFanChannelQuery.convert(page);
        List<AddFanChannel> addFanChannelList = addFanChannelMapper.findByList(page, addFanChannelQuery);
        if(addFanChannelList != null){
            for (AddFanChannel addFanChannel : addFanChannelList){
                AddFanChannelVo addFanChannelVo = new AddFanChannelVo();
                BeanUtils.copyProperties(addFanChannel, addFanChannelVo);

                if(StringUtils.isNotBlank(addFanChannel.getDeviceUniqueIds())){
                    try {
                        List<Long> deviceUniqueIdList = JSONObject.parseArray(addFanChannel.getDeviceUniqueIds(), Long.class);
                        addFanChannelVo.setDeviceUniqueIdList(deviceUniqueIdList);
                    }catch (Exception e){

                    }
                }

                if(StringUtils.isNotBlank(addFanChannel.getSource())){
                    try {
                        List<Integer> sourceList = JSONObject.parseArray(addFanChannel.getSource(), Integer.class);
                        addFanChannelVo.setSourceList(sourceList);
                    }catch (Exception e){

                    }
                }

                addFanChannelVoList.add(addFanChannelVo);
            }
        }
        //返回到页面分页对象
        Page<AddFanChannelVo> pageVo = new Page<>();
        BeanUtils.copyProperties(page, pageVo);
        pageVo.setRecords(addFanChannelVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /**
     * 查询待维护会员
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param addFanChannelIdDto
     * @return null
     */
    public AddFanChannelVo findById(AddFanChannelIdDto addFanChannelIdDto){
        AddFanChannelVo addFanChannelVo = new AddFanChannelVo();
        if(addFanChannelIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }
        AddFanChannel addFanChannel = addFanChannelMapper.selectById(addFanChannelIdDto.getId(), addFanChannelIdDto.getMerId());
        if(addFanChannel != null){
            BeanUtils.copyProperties(addFanChannel, addFanChannelVo);

            if(StringUtils.isNotBlank(addFanChannel.getDeviceUniqueIds())){
                try {
                    List<Long> deviceUniqueIdList = JSONObject.parseArray(addFanChannel.getDeviceUniqueIds(), Long.class);
                    addFanChannelVo.setDeviceUniqueIdList(deviceUniqueIdList);
                }catch (Exception e){

                }
            }

            if(StringUtils.isNotBlank(addFanChannel.getSource())){
                try {
                    List<Integer> sourceList = JSONObject.parseArray(addFanChannel.getSource(), Integer.class);
                    addFanChannelVo.setSourceList(sourceList);
                }catch (Exception e){

                }
            }
        }
        return addFanChannelVo;
    }

    /**
     * 添加进粉渠道状态
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param addFanChannelDto
     * @return null
     */
    @Transactional
    public void addAddFanChannel(AddFanChannelDto addFanChannelDto){
        if(StringUtils.isBlank(addFanChannelDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入渠道名称");
        }

        String deviceType = addFanChannelDto.getDeviceType();
        if(StringUtils.isBlank(deviceType)){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入设备类型");
        }

        List<Integer> sourceList = addFanChannelDto.getSourceList();
        if(sourceList == null || sourceList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择注册来源");
        }

        List<Long> deviceUniqueIdList = addFanChannelDto.getDeviceUniqueIdList();
        if(deviceUniqueIdList == null || deviceUniqueIdList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入进粉设备");
        }

        if(addFanChannelDto.getRegTimeStart() == null || addFanChannelDto.getRegTimeEnd() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入注册时间");
        }

        int now =  DateUtil.getNowTime();
        AddFanChannel addFanChannel = new AddFanChannel();
        addFanChannel.setId(IdWorker.getId());
        addFanChannel.setMerId(addFanChannelDto.getMerId());
        addFanChannel.setName(addFanChannelDto.getName());
        addFanChannel.setInfo(addFanChannelDto.getInfo());
        addFanChannel.setDeviceType(deviceType);
        addFanChannel.setDeviceUniqueIds(JSONObject.toJSONString(deviceUniqueIdList));
        addFanChannel.setSource(JSONObject.toJSONString(sourceList));
        addFanChannel.setRegTimeStart(addFanChannelDto.getRegTimeStart());
        addFanChannel.setRegTimeEnd(addFanChannelDto.getRegTimeEnd());
        addFanChannel.setPrice(Tools.getDouble(addFanChannelDto.getPrice()));
        addFanChannel.setCreateTime(now);
        addFanChannel.setUpdateTime(now);
        addFanChannelMapper.insert(addFanChannel);

        //添加进粉渠道明细
        List<AddFanChannelDetail> addFanChannelDetailList = new ArrayList<>();
        for (Long deviceUniqueId : deviceUniqueIdList) {
            for (Integer source : sourceList) {
                AddFanChannelDetail addFanChannelDetail = new AddFanChannelDetail();
                addFanChannelDetail.setId(IdWorker.getId());
                addFanChannelDetail.setMerId(addFanChannel.getMerId());
                addFanChannelDetail.setAddFanChannelId(addFanChannel.getId());
                addFanChannelDetail.setName(addFanChannel.getName());
                addFanChannelDetail.setInfo(addFanChannel.getInfo());
                addFanChannelDetail.setDeviceType(addFanChannel.getDeviceType());
                addFanChannelDetail.setDeviceUniqueId(deviceUniqueId);
                addFanChannelDetail.setSource(source);
                addFanChannelDetail.setRegTimeStart(addFanChannel.getRegTimeStart());
                addFanChannelDetail.setRegTimeEnd(addFanChannel.getRegTimeEnd());
                addFanChannelDetail.setPrice(Tools.getDouble(addFanChannel.getPrice()));
                addFanChannelDetail.setCreateTime(now);
                addFanChannelDetail.setUpdateTime(now);
                addFanChannelDetailList.add(addFanChannelDetail);
                if(addFanChannelDetailList.size() >= 50){ //超过50个明细，先批量保存一波，免得超长
                    addFanChannelDetailMapper.batchAdd(addFanChannelDetailList);
                    addFanChannelDetailList.clear();
                }
            }
        }

        //最后将剩余的明细在保存一次
        if(addFanChannelDetailList.size() > 0){
            addFanChannelDetailMapper.batchAdd(addFanChannelDetailList);
            addFanChannelDetailList.clear();
        }
    }

    /**
     * 删除进粉渠道信息
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param addFanChannelIdDto
     * @return null
     */
    @Transactional
    public void deleteAddFanChannel(AddFanChannelIdDto addFanChannelIdDto){
        if(addFanChannelIdDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        AddFanChannel addFanChannel = addFanChannelMapper.selectById(addFanChannelIdDto.getId(), addFanChannelIdDto.getMerId());

        if(addFanChannel == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "该记录不存在");
        }

        //删除进粉渠道
        addFanChannelMapper.deleteById(addFanChannelIdDto.getId(), addFanChannelIdDto.getMerId());

        //删除明细
        addFanChannelDetailMapper.deleteByAddFanChannelId(addFanChannel.getId(), addFanChannel.getMerId());
    }


    /**
     * 修改进粉渠道信息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    @Transactional
    public void updateAddFanChannel(AddFanChannelDto addFanChannelDto){

        if(addFanChannelDto.getId() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入id");
        }

        AddFanChannel addFanChannel = addFanChannelMapper.selectById(addFanChannelDto.getId(), addFanChannelDto.getMerId());
        if(addFanChannel == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请进粉渠道不存在");
        }

        if(StringUtils.isBlank(addFanChannelDto.getName())){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入渠道名称");
        }

        String deviceType = addFanChannelDto.getDeviceType();
        if(StringUtils.isBlank(deviceType)){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入设备类型");
        }

        List<Integer> sourceList = addFanChannelDto.getSourceList();
        if(sourceList == null || sourceList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择注册来源");
        }

        List<Long> deviceUniqueIdList = addFanChannelDto.getDeviceUniqueIdList();
        if(deviceUniqueIdList == null || deviceUniqueIdList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入进粉设备");
        }

        if(addFanChannelDto.getRegTimeStart() == null || addFanChannelDto.getRegTimeEnd() == null){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入注册时间");
        }

        int now =  DateUtil.getNowTime();
        addFanChannel.setName(addFanChannelDto.getName());
        addFanChannel.setInfo(addFanChannelDto.getInfo());
        addFanChannel.setDeviceType(deviceType);
        addFanChannel.setDeviceUniqueIds(JSONObject.toJSONString(deviceUniqueIdList));
        addFanChannel.setSource(JSONObject.toJSONString(sourceList));
        addFanChannel.setRegTimeStart(addFanChannelDto.getRegTimeStart());
        addFanChannel.setRegTimeEnd(addFanChannelDto.getRegTimeEnd());
        addFanChannel.setPrice(Tools.getDouble(addFanChannelDto.getPrice()));
        addFanChannel.setUpdateTime(now);
        addFanChannelMapper.updateById(addFanChannel);
        //批量删除进粉渠道明细
        addFanChannelDetailMapper.deleteByAddFanChannelId(addFanChannel.getId(), addFanChannel.getMerId());
        //添加进粉渠道明细
        List<AddFanChannelDetail> addFanChannelDetailList = new ArrayList<>();
        for (Long deviceUniqueId : deviceUniqueIdList) {
            for (Integer source : sourceList) {
                AddFanChannelDetail addFanChannelDetail = new AddFanChannelDetail();
                addFanChannelDetail.setId(IdWorker.getId());
                addFanChannelDetail.setMerId(addFanChannel.getMerId());
                addFanChannelDetail.setAddFanChannelId(addFanChannel.getId());
                addFanChannelDetail.setName(addFanChannel.getName());
                addFanChannelDetail.setInfo(addFanChannel.getInfo());
                addFanChannelDetail.setDeviceType(addFanChannel.getDeviceType());
                addFanChannelDetail.setDeviceUniqueId(deviceUniqueId);
                addFanChannelDetail.setSource(source);
                addFanChannelDetail.setRegTimeStart(addFanChannel.getRegTimeStart());
                addFanChannelDetail.setRegTimeEnd(addFanChannel.getRegTimeEnd());
                addFanChannelDetail.setPrice(Tools.getDouble(addFanChannel.getPrice()));
                addFanChannelDetail.setCreateTime(now);
                addFanChannelDetail.setUpdateTime(now);
                addFanChannelDetailList.add(addFanChannelDetail);
                if(addFanChannelDetailList.size() >= 50){ //超过50个明细，先批量保存一波，免得超长
                    addFanChannelDetailMapper.batchAdd(addFanChannelDetailList);
                    addFanChannelDetailList.clear();
                }
            }
        }
        //最后将剩余的明细在保存一次
        if(addFanChannelDetailList.size() > 0){
            addFanChannelDetailMapper.batchAdd(addFanChannelDetailList);
            addFanChannelDetailList.clear();
        }
    }

}
